﻿using System;
using Tfs.OnTheRoad.ViewModel;
namespace Tfs.OnTheRoad.Commands
{
    /// <summary>
    /// Tests the Access Settings
    /// </summary>
    public class TestSettingsCommand : BaseCommand<SettingsViewModel>
    {
        public TestSettingsCommand(SettingsViewModel owner)
            : base(owner)
        {
            Owner.PropertyChanged += (s, e) =>
            {
                if (CanExecuteChangedSetted && string.Compare(e.PropertyName, "CanTest") == 0)
                {
                    raiseCanExecuteChanged(this, EventArgs.Empty);
                }
            };
        }
        
        public override bool CanExecute(object parameter)
        {
            return Owner.CanTest;
        }

        public override void Execute(object parameter)
        {
            Owner.Login(
                r =>
                {
                    if (!r.IsAuthenticated)
                    {
                        Owner.TestResults = r.Description;
                    }
                    else
                    {
                        Owner.TestResults = "S U C C E S S ! ! !";
                        NavigateTo("/MainPage.xaml");
                    }
                });
        }
    }
}